/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package FileReadWrite;
import Points.*;
/**
 *
 * @author Avishai
 */
//import java.awt.print.Book;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import java.awt.Color;
import javax.swing.JTextField;

/**
 *
 * @author Avishai Kopeliansky & Vered Ovadia
 */
@SuppressWarnings("restriction")
public class Write{
	Document dom;
	List<Point> myData;
        /**
         *Constructor
         * @param pc Point Container
         */
        public Write(PointContainer pc){
		myData = new ArrayList<Point>();
		for(int i=0 ; i < pc.size() ; i++){
			loadData(pc.elementAt(i));
		}             
		createDocument();

	}
        /**
         *File Write Function
         * @param s String - FileName
         * @param txt JTextField  - for output messages
         */
        public void Writer(String s , JTextField txt){
		txt.setText("Started .. ");
		createDOMTree();		
                try{
                Thread.sleep(1500);
		printToFile(s);
                }
                catch(Exception e){};
                txt.setText("Generated file successfully.");
	}
	private void loadData(Point p){
            Color ct = new Color(p.getRed(),p.getGreen(),p.getBlue());
		myData.add(new Point(p.GetX(),p.GetY(),ct));

	}
	private void createDocument() {
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			
			DocumentBuilder db = dbf.newDocumentBuilder();			
			dom = db.newDocument();
		}
		catch(ParserConfigurationException pce) {		
			System.out.println("Error while trying to instantiate DocumentBuilder " + pce);
			System.exit(1);
		}

	}

	private void createDOMTree(){

		
		Element rootEle = dom.createElement("Points");
		dom.appendChild(rootEle);
		Iterator<Point> it  = myData.iterator();
		while(it.hasNext()) {
			Point p = (Point)it.next();
             
			Element pointEle = createPointElement(p);
			rootEle.appendChild(pointEle);
		}

	}
	private Element createPointElement(Point p){

		Element pointEle = dom.createElement("Point");		
		Element xEle = dom.createElement("Xcord");
		Text xText = dom.createTextNode(p.toStringx());
		xEle.appendChild(xText);
		pointEle.appendChild(xEle);

	
		Element yEle = dom.createElement("Ycord");
		Text yText = dom.createTextNode(p.toStringy());
		yEle.appendChild(yText);
		pointEle.appendChild(yEle);

                Element rColor = dom.createElement("rColor");
		Text rcolorText = dom.createTextNode(""+p.getRed());
		rColor.appendChild(rcolorText);
		pointEle.appendChild(rColor);

                Element gColor = dom.createElement("gColor");
		Text gcolorText = dom.createTextNode(""+p.getGreen());
		gColor.appendChild(gcolorText);
		pointEle.appendChild(gColor);

                Element bColor = dom.createElement("bColor");
		Text bcolorText = dom.createTextNode(""+p.getBlue());
		bColor.appendChild(bcolorText);
		pointEle.appendChild(bColor);
		return pointEle;

	}
	private void printToFile(String s){

		try
		{
			OutputFormat format = new OutputFormat(dom);
			format.setIndenting(true);		
			XMLSerializer serializer = new XMLSerializer(
					new FileOutputStream(new File(s)), format);

			serializer.serialize(dom);

		} catch(IOException ie) {
			ie.printStackTrace();
		}
	}


/*
	public static void main(String [] args){
		PointContainer pc = new PointContainer();
		Point a = new Point();
		Point b = new Point(1,2);
		pc.add(a);
		pc.add(b);
		Write wr = new Write(pc);
		wr.runExample();
		System.out.println(a.toStringx());
		System.out.println(a.toStringy());
	}
*/
}